package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Service.WarningService;
import com.ruoyi.StarStudio.Utils.QueryPageUtils;
import com.ruoyi.StarStudio.Domain.*;
import com.ruoyi.StarStudio.Service.MeteorologyService;
import com.ruoyi.StarStudio.Utils.WarningCompareUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@SuppressWarnings("rawtypes")
@RestController
@RequestMapping("/metes")
public class MeteorologyController {

    @Autowired
    private MeteorologyService meteorologyService;

    @Autowired
    private WarningService warningService;

    /**
     * 获取所有气象信息
     * @return 结果对象
     */
    @GetMapping
    public Result findAll(){
        List<Meteorology> allMeteorology;
        try {
            allMeteorology = meteorologyService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.METEOROLOGY_QUERY_SUCCESS, allMeteorology);
    }

    /**
     * 导出气象数据到 Excel表格
     * @param response HTTP 响应对象
     * @param meteorology 气象信息对象
     */
    @Log(title = "气象信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Meteorology meteorology) {
        List<Meteorology> list = meteorologyService.getList(meteorology);
        ExcelUtil<Meteorology> util = new ExcelUtil<>(Meteorology.class);
        util.exportExcel(response, list, "气象数据");
    }

    /**
     * 带搜索功能分页获取气象信息
     * @param meteorology 气象信息对象
     * @return 分页结果对象
     */
    @GetMapping("/page")
    public PageResult getList(Meteorology meteorology){
        QueryPageUtils.startPage();
        List<Meteorology> allMeteorology;
        PageInfo pageInfo;
        try {
            allMeteorology = meteorologyService.getList(meteorology);
            pageInfo = new PageInfo<>(allMeteorology);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.METEOROLOGY_QUERY_SUCCESS, allMeteorology);
    }

    /**
     * 根据气象id获取该条气象数据
     * @param meteorology_id 气象数据ID
     * @return 结果对象 Result
     */
    @GetMapping("/byId/{meteorology_id}")
    public Result getByMeteId(@PathVariable long meteorology_id){
        Meteorology byMeteId;
        try {
            byMeteId = meteorologyService.getByMeteId(meteorology_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.METEOROLOGY_QUERY_SUCCESS, byMeteId);
    }

    /**
     * 获取某个地块的所有气象数据
     * @param zoneId 地块ID
     * @return 结果对象 Result
     */
    @GetMapping("/{zoneId}")
    public Result getAllByZoneId(@PathVariable int zoneId){
        List<Meteorology> meteorologyList;
        try {
            meteorologyList = meteorologyService.getAllByZoneId(zoneId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.METEOROLOGY_QUERY_SUCCESS, meteorologyList);
    }

    /**
     * 分页获取某个地块的所有气象数据
     * @param zone_id 地块ID
     * @return 分页结果对象 PageResult
     */
    @GetMapping("/page/{zone_id}")
    public PageResult getPageByZoneId(@PathVariable Integer zone_id){
        QueryPageUtils.startPage();
        List<Meteorology> meteorologyList;
        PageInfo pageInfo;
        try {
            meteorologyList = meteorologyService.getAllByZoneId(zone_id);
            pageInfo = new PageInfo<>(meteorologyList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.METEOROLOGY_QUERY_SUCCESS, meteorologyList);
    }

    /**
     * 获取某个地块最新的一条气象数据
     * @param zoneId 地块ID
     * @return 结果对象 Result
     */
    @GetMapping("/one/{zoneId}")
    public Result getOneByZoneId(@PathVariable int zoneId){
        Meteorology NewestOneByZoneId;
        Warning warningByZoneId;
        try {
            NewestOneByZoneId = meteorologyService.getOneByZoneId(zoneId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_QUERY_FAIL);
        }

        try {
            warningByZoneId = warningService.getByZoneId(zoneId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.WARNING_QUERY_FAIL);
        }

        String ComRes = WarningCompareUtils.MeteorologyCompare(NewestOneByZoneId, warningByZoneId);
        if (ComRes.length() > 0){
            return new Result(HttpStatusCode.WARNING, ComRes, NewestOneByZoneId);
        } else {
            return new Result(HttpStatusCode.SUCCESS, MessageConstant.METEOROLOGY_QUERY_SUCCESS, NewestOneByZoneId);
        }
    }

    /**
     * 新增某个地块的一条气象数据
     * @param meteorology 气象信息对象
     * @param zoneId 地块ID
     * @return 结果对象 Result
     */
    @PostMapping("/{zoneId}")
    public Result add(@RequestBody Meteorology meteorology, @PathVariable int zoneId) {
        try {
            meteorology.setZone_id(zoneId);
            meteorology.setMeteorology_updateTime(new Timestamp(new Date().getTime()));
            meteorologyService.add(meteorology,zoneId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_ADD_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.METEOROLOGY_ADD_SUCCESS);
    }

    /**
     * 修改一条气象数据
     * @param meteorology 气象信息对象
     * @return 结果对象Result
     */
    @PutMapping
    public Result update(@RequestBody Meteorology meteorology){
        try {
            meteorology.setMeteorology_updateTime(new Timestamp(new Date().getTime()));
            meteorologyService.update(meteorology);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_UPDATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.METEOROLOGY_UPDATE_SUCCESS);
    }

    /**
     * 删除一条气象数据
     * @param meteorologyId 气象数据ID
     * @return 结果对象 Result
     */
    @DeleteMapping("/{meteorologyId}")
    public Result delete(@PathVariable Integer meteorologyId){
        try {
            meteorologyService.delete(meteorologyId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.METEOROLOGY_DELETE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.METEOROLOGY_DELETE_SUCCESS);
    }
}
